package com.dlna.client.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.text.TextUtils;

import com.dlna.client.enity.MenuInfo;
import com.dlna.client.enity.PictureInfo;

/**
 * 
 * @author zhangjun
 *
 * @Time 2015-5-6 10:47
 */
public class XMLHelper {
	
	
	
	public static ArrayList<MenuInfo> parseMenu(XmlPullParser parser,String path){
		String tagName;
		ArrayList<MenuInfo> menuList = null;
		MenuInfo menu = null;
		try {
			int eventType = parser.getEventType();
			while(eventType != XmlPullParser.END_DOCUMENT){
				switch (eventType) {
				case XmlPullParser.START_TAG:
					tagName = parser.getName();
					if("Menus".equals(tagName)){
						menuList = new ArrayList<MenuInfo>();
					}else if("menu".equals(tagName)){
						menu = new MenuInfo();
						menu.menuName = parser.getAttributeValue(0);
						menu.DirPath = path + File.separator + menu.menuName;
						String menusStr = parser.getAttributeValue(1);
						if(!TextUtils.isEmpty(menusStr)){
							menu.menus = new ArrayList<MenuInfo>();
							String[] menusArray = menusStr.split("\\|");
							for (int i = 0; i < menusArray.length; i++) {
								menu.menus.add(new MenuInfo(menusArray[i], menu.DirPath + File.separator + menusArray[i]));
							}
						}
					}
					
					break;
					
				case XmlPullParser.END_TAG:
					tagName = parser.getName();
					if("menu".equals(tagName)){
						menuList.add(menu);
						menu = null;
					}
					break;

				default:
					break;
				}
				eventType = parser.next();
			}
			
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return menuList;
	}

	public static ArrayList<MenuInfo> ParseXML(XmlPullParser parser){
		String tagName;
		ArrayList<MenuInfo> menuList = null;
		ArrayList<PictureInfo> fileList = null;
		MenuInfo menu = null;
		PictureInfo file = null;
		try {
			int eventType = parser.getEventType();
			while(eventType != XmlPullParser.END_DOCUMENT){
				switch (eventType) {
				case XmlPullParser.START_TAG:
					tagName = parser.getName();
					if("Menus".equals(tagName)){
						menuList = new ArrayList<MenuInfo>();
					}else if("menu".equals(tagName)){
						menu = new MenuInfo();
						menu.DirName = parser.getAttributeValue(0);
					}else if("Files".equals(tagName)){
						fileList = new ArrayList<PictureInfo>();
					}else if("File".equals(tagName)){
						file = new PictureInfo();
					}else if("name".equals(tagName)){
						file.filePath = parser.nextText();
					}else if("title".equals(tagName)){
						file.title = parser.nextText();
					}else if("text".equals(tagName)){
						file.text = parser.nextText();
					}
					
					break;
					
				case XmlPullParser.END_TAG:
					tagName = parser.getName();
					if("Files".equals(tagName)){
						menu.picList = fileList;
					}else if("File".equals(tagName)){
						fileList.add(file);
						file = null;
					}else if("menu".equals(tagName)){
						menuList.add(menu);
						menu = null;
					}
					break;

				default:
					break;
				}
				eventType = parser.next();
			}
			
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return null;
	}
	
	public static ArrayList<String> parseUrl(XmlPullParser parser){
		String tagName;
		ArrayList<String> urlList = null;	
		String urlstr="";
		try {
			int eventType = parser.getEventType();
			while(eventType != XmlPullParser.END_DOCUMENT){
				
				switch (eventType) {
				case XmlPullParser.START_TAG:
					tagName = parser.getName();
					if("Urls".equals(tagName)){
						urlList = new ArrayList<String>();
					}else if("url".equals(tagName)){						
						urlstr=parser.getAttributeValue(1);
					}
					break;					
				case XmlPullParser.END_TAG:
					tagName = parser.getName();
					if("url".equals(tagName)){
						urlList.add(urlstr);
						urlstr = "";
					}
					break;

				default:
					break;
				}
				eventType = parser.next();
			}
			
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return urlList;
	}

}
